<template>
  <div class="app-container">
    <el-form  @submit.native.prevent :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item   label="产品ID" prop="keys">
        <el-input
          v-model="queryParams.keys"
          placeholder="请输入产品ID或产品链接"
          value="shelf"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
       </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['alibaba:xp:pl']"
        >批量查询</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主图" align="center" prop="img">
         <template   slot-scope="scope">            
                <el-link  title="点击访问产品"  :href="'https://www.alibaba.com/product-detail/aa_'+scope.row.id+'.html'" target="_blank" class="buttonText"  type="primary" :underline="false"><img width="120px" height="120px" :src="scope.row.img"  />
                  </el-link>

       </template>         
      </el-table-column>
      <el-table-column label="产品ID" align="center" prop="id" />
      <el-table-column label="价格" align="center" prop="Price" />
      <el-table-column label="7天询盘" align="center" prop="a7d" />
      <el-table-column label="30天询盘" align="center" prop="a30d" />
      <el-table-column label="90天询盘" align="center" prop="aall" />
      <!-- <el-table-column label="test" align="center"   :formatter="test"></el-table-column> -->
    </el-table>


    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品信息" prop="keys">
          <el-input v-model="form.keys" type="textarea" placeholder="一行一个产品ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig,xps, addConfig, exportConfig } from "@/api/alibaba/xp";


export default {
  name: "Xp",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      xps:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        keys: "",
        day: "all",
        ids:[]
      },
      // 表单参数
      form: {
        day: "all"
       },
      // 表单校验
      rules: {
        remark: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.$WebSocket.WebSocketBandMsgReceivedEvent(this.WebSocket_OnMesage)
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      // this.queryParams.ids.push(this.queryParams.keys)
         this.queryParams.ids=this.queryParams.keys.split(/[(\r\n)\r\n]+/);
          listConfig(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.configList = response.data;
          console.log(this.configList);
          this.loading = false;
        }
      );
    }, 
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    }, 
    reset() {
      this.form = {
        keys: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "批量查询";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    unique(arr) {
      const res = new Map();
      return arr.filter((arr) => !res.has(arr.id) && res.set(arr.id, 1));
    },
    xpAdd(msg) {
      if (msg.data != undefined){
          var json = eval('(' + msg.data + ')');
          let row = {
            id: json.id,
            img: json.img,
            Price: json.Price,
            a7d: json.a7d,
            a30d: json.a30d,
            aall: json.aall,
          }
          this.configList.push(row) 
        }
    },
    WebSocket_OnMesage(msg) {//实际消息回调事件
      var json = eval('(' + msg.data + ')');
      console.log(json)
      if(json.code==500){
        this.msgError("请十五分钟后再进行批量查询,本次进行被分控,请注意查询频率及数量");
        this.open=false;
      }else{
        this.xpAdd(msg)
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            this.xps["type"]="xps";
            this.xps["ids"]=this.form.keys.split(/[(\r\n)\r\n]+/);
            this.$WebSocket.Send(JSON.stringify(this.xps))
            this.WebSocket_OnMesage(JSON.stringify(this.xps))
            this.open = false;
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportConfig(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>